package org.grottarossa.data.query;

import java.util.ArrayList;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;
import javax.jdo.annotations.Transactional;

import org.apache.log4j.Logger;

public class QueryUtil 
{
	Logger logger = Logger.getLogger(QueryUtil.class.getName());
	
	private PersistenceManagerFactory pmf;
	
	
	public QueryUtil(PersistenceManagerFactory pmf) 
	{
		this.pmf = pmf;
	}
	
	@Transactional
	public ArrayList<?> executeQuery(String jdoSql)
	{
		PersistenceManager pm = pmf.getPersistenceManager(); 
		pm.setDetachAllOnCommit(true);
		ArrayList<?> detach,obj = null;
		
		try{
		   
			Query q =  pm.newQuery(jdoSql);
			obj = (ArrayList<?>) q.execute();
			detach = (ArrayList<?>) pm.detachCopyAll(obj);
			
		}
		finally
		{
			pm.close();	
		
		}
		return detach;	
				
	}
}
